<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;

class Focus extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // var_dump(1);die;
        $article = Db::table('article a')
       // ->join('images i','a.aid = i.aid')
       ->join('user_self u','u.uid = a.uid')
       ->where('dispaly = 1')
       ->order('regtime desc')
       ->select();

       //栏目条
        if (session('uid','','think')) {  
        $ass = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->order('id desc')
        ->limit(10)
       ->select();
            
        } else {
           $ass = null;
        }
        //登录栏目条
        if (session('uid','','think')) {  
        $assz = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->select();
            
        } else {
           $assz = null;
        }
        //随机频道
        $arti = Db::query('SELECT * FROM `column` where pid > 0 ORDER BY RAND() LIMIT 6 ');
        //已经订阅了的
        if (session('uid','','think')) {  
        $list = Db::name('subscribe s')
        ->where('s.uid ='.session('uid','','think'))
       ->field('cid')
       ->select();
            $list2 = array();
            foreach ($list as $key => $v) {
                $list2[] = $v['cid'];
            }
            
                $list3 = null;
                foreach ($list2 as $key => $value) {
                    $list3 .= $value.',';
            }
        } else {
           $list3 = null;
        }

        //未登录栏目
        $hotss = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(9)
        ->select();
        //未登入下栏目
        $hotssz = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(10)
        ->select();
        return view('Focus/index',[
            'article'=> $article,
            'ass' => $ass,
            'assz' => $assz,
            'list3' => $list3,
            'arti' => $arti,
            'hotss' => $hotss,
            'hotssz' => $hotssz,
            ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $ll = Db::name('column')->where('cid',$id)->select();
        if (empty($ll)) {
           return $this->error('数据错误','/index');
        }
        $article = Db::table('article a')
       ->join('user_self u','u.uid = a.uid')
       ->join('column c','a.cid = c.cid')
       ->where('dispaly = 1 and c.cid = '.$id)
       ->order('regtime desc')
       ->select();

        if (session('uid','','think')) {  
        $ass = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->limit(10)
        ->order('id desc')
       ->select();
            
        } else {
           $ass = null;
        }
        //登录栏目条
        if (session('uid','','think')) {  
        $assz = Db::name('subscribe s')
        ->join('column c','s.cid = c.cid')
        ->where('s.uid ='.session('uid','','think'))
        ->select();
            
        } else {
           $assz = null;
        }
        //未登录栏目
        $hotss = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(9)
        ->select();
        //未登入下栏目
        $hotssz = Db::name('column')
        ->where('pid > 0 and hot = 1')
        ->limit(10)
        ->select();

        //订阅频道随机
        $arti = Db::query('SELECT * FROM `column` where pid > 0 ORDER BY RAND() LIMIT 6 ');

        //已经订阅了的
        if (session('uid','','think')) {  
        $list = Db::name('subscribe s')
        ->where('s.uid ='.session('uid','','think'))
       ->field('cid')
       ->select();
            $list2 = array();
            foreach ($list as $key => $v) {
                $list2[] = $v['cid'];
            }
            
                $list3 = null;
                foreach ($list2 as $key => $value) {
                    $list3 .= $value.',';
            }
        } else {
           $list3 = null;
        }

        return view('Focus/index',[
            'article'=> $article,
            'ass' => $ass,
            'assz' => $assz,
            'arti' => $arti,
            'list3' => $list3,
            'hotss' => $hotss,
            'hotssz' => $hotssz,
            ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    
}
